﻿Imports Aricie.DNN.UI.Controls
Imports Lucene.Net.Search
Imports Aricie.ComponentModel
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Aricie.DNN.Modules.LuceneSearch.Logs
Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports Aricie.DNN.Settings

Namespace Aricie.DNN.Modules.LuceneSearch.UI

    Public Enum QueryMode
        Search
        Filter
    End Enum

    Public MustInherit Class LuceneFilterControlBase
        Inherits AricieUserControlBase
        Implements ILuceneFilterControl


        Public Event FilterChanged(ByVal sender As Object, ByVal e As GenericEventArgs(Of LuceneFilterQuery)) Implements ILuceneFilterControl.FilterChanged

        Private _AjaxMode As Boolean

        Private _CustomSortExpression As String = ""
        Private _DisableAllCustomSortExpression As Boolean = False
        Private _EnableFilterLog As Boolean
        Private _CurrentSettings As LuceneResultsSettings

        Public Property AjaxMode() As Boolean
            Get
                Return Me._AjaxMode
            End Get
            Set(ByVal value As Boolean)
                Me._AjaxMode = value
            End Set
        End Property

        Public Property QueryMode() As QueryMode


        Public ReadOnly Property CurrentSettings() As LuceneResultsSettings
            Get
                If _CurrentSettings Is Nothing Then
                    _CurrentSettings = GetModuleSettings(Of LuceneResultsSettings)(SettingsScope.ModuleSettings, Me.ModuleID)
                End If
                Return _CurrentSettings
            End Get
        End Property

        Public Property CustomSortExpression() As String
            Get
                Return _CustomSortExpression
            End Get
            Set(ByVal value As String)
                _CustomSortExpression = value
            End Set
        End Property

        Public Property DisableAllCustomSortExpressionBool() As String
            Get
                Return _DisableAllCustomSortExpression.ToString
            End Get
            Set(ByVal value As String)
                _DisableAllCustomSortExpression = CBool(value)
            End Set
        End Property

        Public Property EnableFilterLog() As Boolean Implements ILuceneFilterControl.EnableFilterLog
            Get
                Return Me._EnableFilterLog
            End Get
            Set(ByVal value As Boolean)
                Me._EnableFilterLog = value
            End Set
        End Property

        Public Overridable Function GetFilterLogElement() As FilterLogElement

            Return New FilterLogElement(Me.GetType.Name, "")
        End Function

        Public Function GetCustomSortExpression() As System.Collections.Generic.IEnumerable(Of String) Implements ILuceneFilterControl.GetCustomSortExpression
            If Me.HasFilter Then
                Return CustomSortExpression.Split(CChar(";"))
            Else
                Return New List(Of String)
            End If
        End Function

        Public Function DisableAllCustomSortExpression() As Boolean Implements ILuceneFilterControl.DisableAllCustomSortExpression
            If Me.HasFilter Then
                Return CBool(DisableAllCustomSortExpressionBool)
            Else
                Return False
            End If
        End Function


        Public MustOverride Function GetFieldNames() As IEnumerable(Of String) Implements ILuceneFilterControl.GetFieldNames

        Public MustOverride Function GetFilter() As LuceneFilterQuery Implements ILuceneFilterControl.GetFilter

        Public MustOverride Function HasFilter() As Boolean Implements ILuceneFilterControl.HasFilter

        Public MustOverride Function ParseFilter(ByVal objFilter As LuceneFilterQuery) As Boolean Implements ILuceneFilterControl.ParseFilter

        Public MustOverride Property Enabled() As Boolean Implements ILuceneFilterControl.Enabled


        Protected Sub OnFilterChanged()
            RaiseEvent FilterChanged(Me, New GenericEventArgs(Of LuceneFilterQuery)(Me.GetFilter))
        End Sub

    End Class

End Namespace


